﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using OpSystem.manage;
using OpSystem.common;

namespace OpSystem.Manage.authority
{
    public partial class AuthorityMenuList : BasePage
    {

        protected string strHtml = "";
        protected string strSystemName = "";
        protected int intSystemID {
            get {
                return RequestUtility.GetInt("sysid", -1);
            }
        }
        protected string strStyle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //SetPageCode(10056);
            if (!Page.IsPostBack)
            {
                SystemBind();
                MyBind(intSystemID);
                if (intSystemID>0)
                {
                    this.ddlSystem.SelectedValue = intSystemID.ToString();
                }
            }
        }
   

        private void MyBind(int sysId)
        {
            IList<AuthorityMenuInfo> list = AuthorityMenuOperate.QueryList(a => a.IsActive == 1 && a.SystemID == sysId);
            var ParentList = from AuthorityMenu in list
                             where AuthorityMenu.ParentID == 0
                             select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl,MenuCode = AuthorityMenu.MenuCode };


            StringBuilder sb = new StringBuilder();

            foreach (var pv in ParentList)
            {
                sb.AppendFormat("<tr><th><i class=\"bookmark\"></i>{0}(URL:{1}) MenuCode:{2}</th>", pv.MenuName, pv.MenuUrl,pv.MenuCode);
                sb.AppendLine("<td><ul class=\"xg\">");
                sb.AppendFormat("<li onclick=\"BMCW.showTopLayer( '添加子菜单','" + GetPageUrl("AuthorityMenuEdit.aspx") +"?cid={0}&sysid={1}', 700, 400)\" ><span class=\"gou\">√</span>添加子项</li>", pv.MenuID, sysId);
                sb.AppendFormat("<li onclick=\"BMCW.showTopLayer('修改菜单', '" + GetPageUrl("AuthorityMenuEdit.aspx") + "?ID={0}&sysid={1}', 700, 400)\" ><span class=\"gou\">√</span>修改</li>", pv.MenuID, sysId);
                sb.AppendFormat("<li onclick=\"BMCW.DataConfirmMaskDel('delmenu','{0}','','" + GetPageUrl("AuthorityMenuList.aspx") + "?sysid={1}' );\" ><span class=\"gou\">√</span>删除</li>", pv.MenuID, sysId);
                sb.AppendLine("</ul>");
                sb.AppendLine("</td>");
                sb.AppendLine(" </tr>");

                var ChildList = from AuthorityMenu in list
                                where AuthorityMenu.ParentID == pv.MenuID
                                select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl, MenuCode = AuthorityMenu.MenuCode };

                foreach (var cv in ChildList)
                {
                    sb.AppendLine(" <tr>");
                    sb.AppendFormat("<td><i class=\"bookmark three\"></i>{0}（URL：{1}）MenuCode:{2}</td>", cv.MenuName, cv.MenuUrl, cv.MenuCode);
                    sb.AppendLine("<td><ul class=\"xg\">");
                    sb.AppendFormat("<li  onclick=\"BMCW.showTopLayer('修改菜单','" + GetPageUrl("AuthorityMenuEdit.aspx") + "?id={0}&sysid={1}',700, 400)\"><span class=\"gou\">√</span>修改</li>", cv.MenuID, sysId);
                    sb.AppendFormat("<li onclick=\"BMCW.DataConfirmMaskDel('delmenu','{0}','','" + GetPageUrl("AuthorityMenuList.aspx") + "?sysid={1}');\"><span class=\"gou\">√</span>删除</li>", cv.MenuID, sysId);
                    sb.AppendLine("</ul>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");

                }
            }
            strHtml = sb.ToString();

        }

        private void SystemBind()
        {
            IList<AuthoritySystemInfo> list = AuthoritySystemOperate.QueryList(a=>a.IsActive==1);
            this.ddlSystem.DataSource = list;
            this.ddlSystem.DataTextField = "SystemName";
            this.ddlSystem.DataValueField = "SystemID";
            this.ddlSystem.DataBind();
            ddlSystem.Items.Insert(0, new ListItem("请选择系统", "0"));

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int sysId = int.Parse(this.ddlSystem.SelectedValue);
            MyBind(sysId);
        }
    }
}
